<?php
class Admin_NewscategoryController extends Zend_Controller_Action{
	
private $_model;
	public function init(){
		$this->_model = new Application_Model_DbTable_NewsCategory();		
	}
	
	public function indexAction(){
		$filters = array();
		
		//từ khóa
		$keyword = $this->_request->getParam('keyword','');
		$filters['keyword']=$keyword;
		$this->view->keyword = $keyword;
		
		
		
		// đến ngày
		$toDate = $this->_request->getParam('toDate',null);
		$filters['toDate']=Cms_String_String::getDateFromString($toDate,'last');//chuyển về định dạng của mysql
		$this->view->toDate = $toDate;
		
		// trạng thái | status
		$status = $this->_request->getParam('searchStatus',"");
		$filters['status']=$status;
		$this->view->status=$status;
		
		// sắp xếp | order
		$order = $this->_request->getParam('order',"");
		if($order!=""){
			list($orderBy,$orderType) = explode('-', $order);
		}else{
			$orderBy=null;
			$orderType=null;
		}
		
		
		//get item count per page
		$icpp = $this->_request->getParam('icpp',20);
		
		//get current page number
		$currentPage = $this->_request->getParam('page',1);
		
		$list = $this->_model->getList($filters,$orderType,$orderBy);
		
		// pagination
		$paginator = Zend_Paginator::factory($list);
		$paginator->setCurrentPageNumber($currentPage);
		$paginator->setItemCountPerPage($icpp);
		$paginator->setPageRange(5);
		
		//assign to view
		$this->view->paginator=$paginator;
	}
	
	public function insertAction(){
		$form = new Admin_Form_NewsCategory();
		if($this->_request->isPost()){
			if($form->isValid($this->_request->getPost())){
		
				$row = $this->_model->createRow($this->_request->getPost());
				$row->news_category_alias = Cms_String_String::toAlias($row->news_category_name_vi);
				$row->news_category_creation_time = date('Y-m-d H:i:s');
		
				// save to database
				$row->save();
				$this->_helper->flashMessenger('Thêm công thành công!');
				$this->_helper->redirector('index','newscategory','admin');
			}
		}
		$this->view->form = $form;
	}
	
	public function updateAction(){
		//get id from request
		$id = $this->_request->getParam('id');
		
		//lấy đối đượng dựa vào id
	
		$row = $this->_model->find($id)->current();
		
		if(!$row){
			echo "<p align=center>Dữ liệu không tồn tại!</p>";
		}else{
			$form = new Admin_Form_NewsCategory();
			if($this->_request->isPost()){
				if($form->isValid($_POST)){
					
					$row->setFromArray($_POST);
					$row->news_category_alias = Cms_String_String::toAlias($row->news_category_name_vi);
					$row->save();
					$this->_helper->flashMessenger('Cập nhật sản phẩm thành công!');
					$this->_helper->redirector('index','newscategory','admin');
				}
			}
			
			$form->populate($row->toArray());
			
			$this->view->form = $form;
		}
	}
	
	public function  deleteAction(){
		$status = 0;
		$id = @(array)$this->_request->getParam('id');
		
		$rows = $this->_model->fetchAll($this->_model->select()->where('news_category_id IN (?)',$id));
		if(count($rows)>0){
			foreach($rows as $row){
				
				$row->delete();
				$status = 1;
			}
			$this->_helper->flashMessenger('Xóa dữ liệu thành công!');
			$this->_helper->redirector('index','newscategory','admin');
		}
	}
	
	public function changestatusAction(){
		$status ="";
		$id = $this->_request->getParam('id');
	
		$row = $this->_model->find($id)->current();
	
		if(!$row){
			$status['error']="Phân loại tin không tồn tại";
		}else{
			$row->news_category_status =(int)(1 - $row->news_category_status);
			$status['status']=$row->news_category_status;
			try {
				$row->save();
			} catch (Exception $e) {
				$status['errors']="lỗi {$e->getMessage()}";
			}
				
		}
		$this->_helper->viewRenderer->setNoRender(true);
		$this->_helper->layout()->disableLayout();
		$this->_helper->json->sendJson($status);
	}
}